package controller;

import java.util.Observable;
import java.util.Observer;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;

import miniMax.TicTacToeFramework;
import simpleTicTacToe.SimpleGame;

import computerPlayer.Player;

/**
 * controller The controller
 * 
 * @author tim
 * 
 */
public final class PlayTicTacToe implements Controlleriface, Observer {

	public PlayTicTacToe() {
		/* The first time, the beginner will be diced */

		model = new TicTacToeFramework();
		view = new SimpleGame(this, model);

		model.addObserver(this);
	}

	public void gameLoop() {
		if (win == -1) {
			if (actualPlayer.isArtificalPlayer()) {
				view.deactivate();
				Timer timer = new Timer();
				timer.schedule(new TimerTask() {

					@Override
					public void run() {
						int tmp = actualPlayer.makeMove();
						view.activate();
						view.getButtons().get(tmp).doClick();
					}
				}, 1000);

			}
		}
	}

	// The model
	private static TicTacToeFramework model = TicTacToeFramework
			.getTicTacToeFrameWork();
	// The view

	// The members
	private Player one;
	private Player two;
	private Player actualPlayer;

	/* Some vars */
	private int input = -1;
	private int win = -1;

	private int[] wins = new int[3];

	/* View */
	private SimpleGame view;
	
	public void resetWins(){
		wins = new int[3];
		view.actualizeWinStat(wins);
		view.actualize(model);
	}
	
	public void updateWins(int win){
		if (win == 0) {
			int tmp = wins[1];
			wins[1] = (tmp + 1);
		} else {
			if (actualPlayer == one) {
				int tmp = wins[0];
				wins[0] = (tmp + 1);
			} else {
				int tmp = wins[2];
				wins[2] = (tmp + 1);
			}
		}
		
		view.actualizeWinStat(wins);
		view.actualize(model);
	}

	public void saveWins() {
		
		System.out.println(wins[0] + "|" + wins[1] + "|" + wins[2]);
	}

	public Player getActualPlayer() {
		return actualPlayer;
	}

	public int getInput() {
		return input;
	}

	public void setInput(int input) {
		this.input = input;
	}

	public void resetGame() {
		/* Reset model */
		model.resetGamefield();
		/* Reset the player */
		getOne().reset(getTicTacToe());
		getTwo().reset(getTicTacToe());
		/* Reset vars */
		input = -1;
		win = -1;
		
		/* Reset view*/
		view.actualize(getTicTacToe());
	}

	public Player getOne() {
		return one;
	}

	public void setOne(Player one) {
		this.one = one;
	}

	public Player getTwo() {
		return two;
	}

	public void setTwo(Player two) {
		this.two = two;
	}

	public static TicTacToeFramework getTicTacToe() {
		return model;
	}

	public static void setTicTacToe(TicTacToeFramework ticTacToe) {
		PlayTicTacToe.model = ticTacToe;
	}

	public void setActualPlayer(Player actualPlayer) {
		this.actualPlayer = actualPlayer;
	}

	public SimpleGame getView() {
		return view;
	}

	public void setView(SimpleGame view) {
		this.view = view;

	}

	public static void main(String[] args) {
		new PlayTicTacToe();
	}

	public void update(Observable o, Object arg) {
		view.update(arg);
	}

	public void switchPlayer() {
		if (actualPlayer.equals(getOne())) {
			actualPlayer = getTwo();
		} else {
			actualPlayer = getOne();
		}
	}

	public int getIndex(JButton jb) {
		return view.getIndex(jb);
	}

	public void setNameLabel(String string) {
		view.setNameLabel(string);
	}

	public void deactivate() {
		view.deactivate();
	}
	
	public void activate(){
		view.activate();
	}

	public void actualize() {
		view.actualize(model);
	}

}
